home *** CD-ROM | disk | FTP | other *** search
/ C++ für Kids / C++ for kids.iso / SETUP / US / CBUILDER / DATA.Z / VECTOR.H < prev    next >
C/C++ Source or Header  |  1997-02-13  |  33KB  |  1,091 lines

  1. #ifndef __STD_VECTOR__
  2. #define __STD_VECTOR__
  3. /* $Revision:   8.1  $ */
  4.  
  5. /***************************************************************************
  6.  *
  7.  * vector - declarations for the Standard Library vector class
  8.  *
  9.  * $Id: vector,v 1.41 1995/09/14 23:53:38 lijewski Exp $
  10.  *
  11.  ***************************************************************************
  12.  *
  13.  * Copyright (c) 1994
  14.  * Hewlett-Packard Company
  15.  *
  16.  * Permission to use, copy, modify, distribute and sell this software
  17.  * and its documentation for any purpose is hereby granted without fee,
  18.  * provided that the above copyright notice appear in all copies and
  19.  * that both that copyright notice and this permission notice appear
  20.  * in supporting documentation.  Hewlett-Packard Company makes no
  21.  * representations about the suitability of this software for any
  22.  * purpose.  It is provided "as is" without express or implied warranty.
  23.  *
  24.  *
  25.  ***************************************************************************
  26.  *
  27.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  28.  * ALL RIGHTS RESERVED
  29.  *
  30.  * The software and information contained herein are proprietary to, and
  31.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  32.  * intends to preserve as trade secrets such software and information.
  33.  * This software is furnished pursuant to a written license agreement and
  34.  * may be used, copied, transmitted, and stored only in accordance with
  35.  * the terms of such license and with the inclusion of the above copyright
  36.  * notice.  This software and information or any other copies thereof may
  37.  * not be provided or otherwise made available to any other person.
  38.  *
  39.  * Notwithstanding any other lease or license that may pertain to, or
  40.  * accompany the delivery of, this computer software and information, the
  41.  * rights of the Government regarding its use, reproduction and disclosure
  42.  * are as set forth in Section 52.227-19 of the FARS Computer
  43.  * Software-Restricted Rights clause.
  44.  *
  45.  * Use, duplication, or disclosure by the Government is subject to
  46.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  47.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  48.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  49.  * P.O. Box 2328, Corvallis, Oregon 97339.
  50.  *
  51.  * This computer software and information is distributed with "restricted
  52.  * rights."  Use, duplication or disclosure is subject to restrictions as
  53.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  54.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  55.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  56.  * then the "Alternate III" clause applies.
  57.  *
  58.  **************************************************************************/
  59.  
  60. #include <stdcomp.h>
  61.  
  62. #include <function>
  63. #include <iterator>
  64. #include <algorith>
  65.  
  66. #ifndef Allocator
  67. #define Allocator allocator
  68. #include <memory>
  69. #endif
  70.  
  71. #ifndef vector
  72. #define vector vector
  73. #endif
  74.  
  75. #ifndef RWSTD_NO_NAMESPACE
  76. namespace std {
  77. #endif
  78.  
  79. template <class T>
  80. class vector
  81. {
  82.   public:
  83.     //
  84.     // Types.
  85.     //
  86.     typedef Allocator<T>                               vector_allocator;
  87.     typedef typename vector_allocator::reference       reference;
  88.     typedef typename vector_allocator::const_reference const_reference;
  89.     typedef typename vector_allocator::pointer         iterator;
  90.     typedef typename vector_allocator::const_pointer   const_iterator;
  91.     typedef typename vector_allocator::size_type       size_type;
  92.     typedef typename vector_allocator::difference_type difference_type;
  93.     typedef          T                                 value_type;
  94.     typedef reverse_iterator<const_iterator, value_type,
  95.                              const_reference, difference_type>
  96.                                                        const_reverse_iterator;
  97.     typedef reverse_iterator<iterator, value_type,
  98.                              reference, difference_type>
  99.                                                        reverse_iterator;
  100.   protected:
  101.  
  102.     Allocator<T> the_allocator;
  103.  
  104.     iterator start;
  105.     iterator finish;
  106.     iterator end_of_storage;
  107.  
  108.     void insert_aux (iterator position, const T& x);
  109.  
  110.   public:
  111.     //
  112.     // construct/copy/destroy
  113.     //
  114.     vector () : start(0), finish(0), end_of_storage(0) {}
  115.     //
  116.     // Build a vector of size n with each element set to default for type T.
  117.     // This requires that T have a default constructor.
  118.     //
  119.     explicit vector (size_type n)
  120.     {
  121.         start = the_allocator.allocate(n);
  122.         T value;
  123.         uninitialized_fill_n(start, n, value);
  124.         finish = start + n;
  125.         end_of_storage = finish;
  126.     }
  127.     //
  128.     // Build a vector of size n with each element set to copy of value.
  129.     //
  130.     explicit vector (size_type n, const T& value)
  131.     {
  132.         start = the_allocator.allocate(n);
  133.         uninitialized_fill_n(start, n, value);
  134.         finish = start + n;
  135.         end_of_storage = finish;
  136.     }
  137.     vector (const vector<T>& x)
  138.     {
  139.         start = the_allocator.allocate(x.end() - x.begin());
  140.         finish = uninitialized_copy(x.begin(), x.end(), start);
  141.         end_of_storage = finish;
  142.     }
  143.  
  144. #ifndef RWSTD_NO_MEMBER_TEMPLATES
  145. template<class InputIterator>
  146.     vector (InputIterator first, InputIterator last)
  147. #else
  148.     vector (const_iterator first, const_iterator last)
  149. #endif
  150.      {
  151.         size_type n;
  152.         __initialize(n, size_type(0));
  153.         distance(first, last, n);
  154.         start = the_allocator.allocate(n);
  155.         finish = uninitialized_copy(first, last, start);
  156.         end_of_storage = finish;
  157.     }
  158.  
  159.     ~vector ()
  160.     {
  161.         destroy(start, finish); the_allocator.deallocate(start);
  162.     }
  163.     vector<T>& operator= (const vector<T>& x);
  164.  
  165. #ifndef RWSTD_NO_MEMBER_TEMPLATES
  166.     template<class InputIterator>
  167.     void assign (InputIterator first, InputIterator last)
  168. #else
  169.     void assign (const_iterator first, const_iterator last)
  170. #endif
  171.     {
  172.         erase(begin(), end()); insert(begin(), first, last);
  173.     }
  174.     //
  175.     // Assign n copies of default value of type T to vector.
  176.     // This requires that T have a default constructor.
  177.     //
  178. #ifndef RWSTD_NO_MEMBER_TEMPLATES
  179.     template<class Size, class T>
  180.     void assign (Size n)
  181. #else
  182.     void assign (size_type n)
  183. #endif
  184.     {
  185.         erase(begin(), end()); insert(begin(), n, T());
  186.     }
  187.     //
  188.     // Assign n copies of t to this vector.
  189.     //
  190. #ifndef RWSTD_NO_MEMBER_TEMPLATES
  191.     template<class Size, class T>
  192.     void assign (Size n, const T& t)
  193. #else
  194.     void assign (size_type n, const T& t)
  195. #endif
  196.     {
  197.         erase(begin(), end()); insert(begin(), n, t);
  198.     }
  199.     //
  200.     // Iterators.
  201.     //
  202.     iterator       begin ()       { return start;  }
  203.     const_iterator begin () const { return start;  }
  204.     iterator       end ()         { return finish; }
  205.     const_iterator end ()   const { return finish; }
  206.  
  207.     reverse_iterator rbegin ()
  208.     {
  209.         reverse_iterator tmp(end()); return tmp;
  210.     }
  211.     const_reverse_iterator rbegin () const
  212.     {
  213.         const_reverse_iterator tmp(end()); return tmp;
  214.     }
  215.     reverse_iterator rend ()
  216.     {
  217.         reverse_iterator tmp(begin()); return tmp;
  218.     }
  219.     const_reverse_iterator rend () const
  220.     {
  221.         const_reverse_iterator tmp(begin()); return tmp;
  222.     }
  223.  
  224.     //
  225.     // Capacity.
  226.     //
  227.     size_type size ()     const { return size_type(end() - begin()); }
  228.     size_type max_size () const { return the_allocator.max_size();   }
  229.     void resize (size_type new_size);
  230.     void resize (size_type new_size, T value);
  231.  
  232.     size_type capacity () const { return size_type(end_of_storage - begin()); }
  233.     bool      empty ()    const { return begin() == end();                    }
  234.     void reserve (size_type n)
  235.     {
  236.         if (capacity() < n)
  237.         {
  238.             iterator tmp = the_allocator.allocate(n);
  239.             uninitialized_copy(begin(), end(), tmp);
  240.             destroy(start, finish);
  241.             the_allocator.deallocate(start);